/*
 * @Author: Mr.Cong Wei
 * @Date: 2022-07-26 20:43:10
 * @LastEditTime: 2022-07-26 22:14:47
 */
const cheerio = require('cheerio')
const cliProgress = require('cli-progress')

function convertHtml(data) {
    const $ = cheerio.load(data)
    const title = $('#info h1').text()
    const list = []
    let amount = 0
    const bar = new cliProgress.SingleBar({
        format: '获取目录中：[{bar}] {percentage}% | 预估时间: {eta}s | {value}/{total}',
        barIncompleteChar: '-'
    }, cliProgress.Presets.rect)
    bar.start($('#list a').length, amount) // 初始化总数，当前数
    $('#list a').each((idx, item) => {
        bar.increment() // +1
        bar.update(amount += 1)
        list.push({
            index: idx,
            url: item.attribs.href,
            title: item.children[0].data
        })
    })
    return {
        title,
        list,
    }
}

function convertContent(data) {
    const $ = cheerio.load(data)
    const content = $('#content').text()
    return content
}


module.exports = { convertHtml, convertContent }